package com.example.onlinemusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlinemusic.common.R;
import com.example.onlinemusic.mapper.ConsumerMapper;
import com.example.onlinemusic.model.domain.Consumer;
import com.example.onlinemusic.model.request.ConsumerRequest;
import com.example.onlinemusic.service.ConsumerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Autowired
    ConsumerMapper consumerMapper;

    @Override
    public R allUser(IPage page) {
        // List<Consumer> consumers = (List<Consumer>) consumerMapper.selectPage(page,null);
        // return R.success("查询成功", consumers);
        return R.success("查询成功", consumerMapper.selectPage(page, null));
    }

    @Override
    public R deleteUser(int id) {
        int i = consumerMapper.deleteById(id);
        if (i > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R deleteByIds(String[] ids) {
        // 把数组转成字符串
        StringBuilder sb = new StringBuilder();
        for (String id : ids) {
            sb.append(id + ",");
        }
        String str = sb.toString();
        // 截掉最后一个逗号
        String newId = str.substring(0, str.length() - 1);
        int result = consumerMapper.deleteByIds(newId);
        if (result > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R userDetail(int id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return R.success("查询成功", consumerMapper.selectList(queryWrapper));
    }

    @Override
    public R allUser() {
        return R.success("查询成功", consumerMapper.selectList(null));
    }

    @Override
    public R loginStatus(ConsumerRequest consumerRequest, HttpSession session) {
        String username = consumerRequest.getUsername();
        String password = consumerRequest.getPassword();
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        Long i = consumerMapper.selectCount(queryWrapper);
        if (i > 0) {
            // session.setAttribute("username",username);
            // return R.success("登陆成功",consumerMapper.selectList(queryWrapper));

            session.setAttribute("username", username);
            Consumer consumer = new Consumer();
            consumer.setUsername(username);
            return R.success("登录成功", consumerMapper.selectList(new QueryWrapper<>(consumer)));
        } else {
            return R.error("登录失败");
        }
    }

    /**
     * 用户注册
     *
     * @param consumerRequest
     * @return
     */
    @Override
    public R addUser(ConsumerRequest consumerRequest) {
        // 注册的逻辑
        // 1.判断前端请求的数据中当中必填字段不能为空
        String username = consumerRequest.getUsername();
        String password = consumerRequest.getPassword();
        Byte sex = consumerRequest.getSex();
        Date birth = consumerRequest.getBirth();
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || sex == null || birth == null) {
            return R.error("请检查数据！");
        }
        // 2.检验不能存在重复的用户名
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        if (consumerMapper.selectCount(queryWrapper) > 0) {
            return R.error("用户名已重复，请重新注册！");
        }
        // 3.将前端请求的数据转换为数据库表consumer对应的实体模型
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerRequest, consumer);
        consumer.setAvator("img/avatorImages/user.jpg");
        consumer.setCreateTime(new Date());
        consumer.setUpdateTime(new Date());
        // 4.插入到数据库中
        if (consumerMapper.insert(consumer) > 0) {
            return R.success("注册成功");
        } else {
            return R.error("注册失败");
        }
    }

    @Override
    public R updateUser(ConsumerRequest consumerRequest) {
        // todo 更新的业务逻辑
        // 1.检验用户是否存在，如果存在采取执行更新的操作，如果不存在就提示错误信息
        // 2.将前端请求的数据转换为数据库表consumer对应的实体模型
        // 3.更新数据库

        return null;
    }


}
